package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.Status;
import com.gxa.entity.SysProviderProgress;
import com.gxa.mapper.ProviderFollowMapper;
import com.gxa.service.ProviderFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProviderFollowServiceImpl implements ProviderFollowService {

    @Autowired
    ProviderFollowMapper providerFollowMapper;

    @Override
    public R findFollowList(Integer currentPage, Integer pageSize) {

        R<Object> r = new R<>();

        if (currentPage != null && pageSize != null) {
            PageHelper.startPage(currentPage,pageSize);
            List<Object> followList = providerFollowMapper.findFollowList();
            PageInfo<SysProviderProgress> pageInfo = new PageInfo(followList);
            r.setData(pageInfo);
            r.fromStatus(Status.SUCCESS);
        }else {
            r.fromStatus(Status.BAD_QUERY_PARAM);
        }

        return r;
    }

    @Override
    public boolean addFollow(SysProviderProgress sysProviderProgress) {

        int i = providerFollowMapper.addFollow(sysProviderProgress);

        return i>0?true:false;
    }

    @Override
    public R updateFollowById(SysProviderProgress sysProviderProgress) {
        R r = new R();
        try {
            r.setCode(200);
            r.setMsg("修改成功");
            r.setData(providerFollowMapper.updateFollowById(sysProviderProgress));
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public PageInfo<SysProviderProgress> findFollowListByName(String name,Integer currentPage,Integer pageSize) {
        List followListByName = providerFollowMapper.findFollowListByName(name);
        PageInfo<SysProviderProgress> pageInfo = new PageInfo<>(followListByName);
        return pageInfo;
    }

    /**
     * 跟进id删除跟进
     * @param id
     * @return
     */
    @Override
    public R deleteFollowById(Integer id) {
        R r = new R();
        try {
            r.setCode(200);
            r.setMsg("修改成功");
            r.setData(providerFollowMapper.deleteFollowById(id));
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("服务器开小差了，请联系管理员");
        }
        return r;
    }
}
